﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using UnityEngine;

 public class TableDataBase
{
    public int ID;
}
//表格基类
public class ConfigTable<TDataBase,T>:Singleton<T> where T : new()  where TDataBase :TableDataBase, new()
{
    //ID,数据条目
    Dictionary<int, TDataBase> _cache = new Dictionary<int, TDataBase>();

    protected void Load (string tablePath)
    {
        MemoryStream tableStream;
#if UNITY_EDITOR

        //开发期，读Project/Config
        var srcPath = Application.dataPath + "/../" + tablePath;
        tableStream = new MemoryStream(File.ReadAllBytes(srcPath));
#else
        //发布之后，读Resources/Config       
        //读表
        //获取文件
        var table = Resources.Load<TextAsset>(tablePath);
        //创建流
        var tableStream = new MemoryStream(table.bytes);
#endif
        //读表,用using语句块自动关闭流
        using (var reader = new StreamReader(tableStream, Encoding.GetEncoding("gb2312")))
        {
            //获取第一行的字段名ID，Name,ModelPath
            var fieldNameStr=  reader.ReadLine();
            //筛选 ,变成数组,比如[0]=ID
            var fieldNameArray = fieldNameStr.Split(",");

            List<FieldInfo> allFieldInfo = new List<FieldInfo>();

            foreach (var fieldName in fieldNameArray)
            {
                //用遍历增加数组里的每一个字段
                allFieldInfo.Add(typeof(TDataBase).GetField(fieldName));
                //便于理解的写法如下
                //Type myType = typeof(TDataBase);
               // FieldInfo myFieldInfo = myType.GetField(fieldName);
                //allFieldInfo.Add(myFieldInfo);

            }
            //正式接收表数据
            //第一条数据
            var lineStr = reader.ReadLine();
            //数据处理
            while (lineStr != null)
            {
                //处理读的第二行
                TDataBase roleDB = readLine(allFieldInfo, lineStr);

                _cache[roleDB.ID] = roleDB;

                //继续读下一行数据，循环上面的操作
                lineStr = reader.ReadLine();
            }


        }
    }

    private static TDataBase readLine(List<FieldInfo> allFieldInfo, string lineStr)
    {
        //数据处理
        //数据表该第二行的字符串用“，”筛选，变成数组
        var itemStrArray = lineStr.Split(",");
        //实例化一个角色数据结构
        var roleDB = new TDataBase();

        //对每个字段
        //foreach(var field in allFieldInfo)
        for (int i = 0; i < allFieldInfo.Count; ++i)
        {
            var field = allFieldInfo[i];
            var data = itemStrArray[i];
            //判断字段类型并设值
            if (field.FieldType == typeof(int))
            {
                field.SetValue(roleDB, int.Parse(data));
            }
            else if (field.FieldType == typeof(string))
            {
                field.SetValue(roleDB, data);
            }
            else if (field.FieldType==typeof(float))
            {
                field.SetValue(roleDB, float.Parse(data));
            }
            else if (field.FieldType==typeof(bool))
            {
                field.SetValue(roleDB, bool.Parse(data));
            }
            else if (field.FieldType==typeof(List<int>))
            {
                var list = new List<int>();
                foreach (var arrayItem in data.Split("$"))
                {
                    list.Add(int.Parse(arrayItem));
                }
                field.SetValue(roleDB, list);
            }
            else if (field.FieldType == typeof(List<float>))
            {
                var list = new List<float>();
                foreach (var arrayItem in data.Split("$"))
                {
                    list.Add(float.Parse(arrayItem));
                }
                field.SetValue(roleDB, list);
            }
            else if (field.FieldType == typeof(List<string>))
            {
                var list = new List<string>(data.Split("$"));
               
                field.SetValue(roleDB, list);
            }
        }
        return roleDB;
        //数组传给RoleDataBase这个类里面的具体变量
        //roleDB.ID = int.Parse(itemStrArray[0]);
        //调用方法传参
        // ProcessData(roleDB,itemStrArray);
        //roleDB.Name = itemStrArray[1];
        //roleDB.ModelPath = itemStrArray[2];
        //将当前roleDB这个类型与当前roleDB绑定，比如_cache[1]对应的就是当前的roleDB这个类里面的数据

    }

    //protected virtual void ProcessData(TDataBase roleDB, string[] itemStrArray)
    //{
    //throw new NotImplementedException();
    //}
    //获取表格第一行的数据
    //索引，返回一个字典中的Key键为索引[int index]比如[1]时的TDataBase对象，对象内含有角色数据
    public TDataBase this[int index]
    {
        get
        {
            TDataBase db;
            _cache.TryGetValue(index, out db);
            return db;
        }
    }
    //获取所有表格数据
    public Dictionary<int, TDataBase> GetAll()
    {
        return _cache;
    }
}
